package com.dabaozhan.modules.wx_app.service.impl;

import com.dabaozhan.common.exception.RRException;
import com.dabaozhan.common.utils.Utils;
import com.dabaozhan.modules.wx_app.dao.UserMapper;
import com.dabaozhan.modules.wx_app.entity.UserEntity;
import com.dabaozhan.modules.wx_app.entity.UserUnionEntity;
import com.dabaozhan.modules.wx_app.enums.ResponseCodeEnum;
import com.dabaozhan.modules.wx_app.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public int saveUser(UserEntity v) {
        if (v.getId() == null) {
            v.setId(Utils.id().toString());
        }
        return userMapper.save(v);
    }

    @Override
    public int updateUser(UserEntity v) {
        return userMapper.update(v);
    }

    @Override
    public UserEntity findByUserId(String id) {

        return userMapper.getObjectById(id);
    }

    @Override
    public UserEntity findByPhone(String telephone) {

        return userMapper.findByPhone(telephone);
    }

    /**
     * fenggang
     */
	@Override
	public UserUnionEntity getUserInfoByPhone(String phone) {
		UserUnionEntity entity= userMapper.getUserInfoByPhone(phone);
		if(null == entity){
			throw new RRException(ResponseCodeEnum.ResponseCode_400.getCode(),ResponseCodeEnum.ResponseCode_400.getDescription());
		}
		return entity;
	}

    @Override
    public UserEntity findByCardUserId(String id) {
        return userMapper.findByCardUserId(id);
    }
}
